package m.jclecai.com.adapter.holder;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.bean.Content;
import m.jclecai.com.listener.NotifyTextViewSetChanged;

/**
 * Created by ZD on 2017/6/3.
 */

public class BetScf_RjcHolder extends ViewHolder {

    @BindView(R.id.tv_homeName)
    TextView tv_homeName;
    @BindView(R.id.tv_guestName)
    TextView tv_guestName;
    @BindView(R.id.tv_jjcProjectOne)
    CheckBox tv_jjcProjectOne;
    @BindView(R.id.tv_jjcProjectTwo)
    CheckBox tv_jjcProjectTwo;
    @BindView(R.id.tv_jjcProjectThree)
    CheckBox tv_jjcProjectThree;
    @BindView(R.id.cb_dan)
    CheckBox cbDan;
    Content mItem;
    List<Content> mData;
    NotifyTextViewSetChanged changed;

    public BetScf_RjcHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
    }

    public void init(List<Content> data, NotifyTextViewSetChanged changed, int position, String perfix) {
        this.mData = data;
        this.mItem = mData.get(position);
        this.changed = changed;
        tv_homeName.setText(mItem.getHome_team());
        tv_guestName.setText(mItem.getAway_team());
        tv_jjcProjectOne.setText(mItem.getOdd_home());
        tv_jjcProjectOne.setChecked(mItem.isOdd_home_select());
        tv_jjcProjectOne.setTag("0_" + perfix + "_0_" + mItem.getOdd_home() + "_胜_3");

        tv_jjcProjectTwo.setText(mItem.getOdd_draw());
        tv_jjcProjectTwo.setChecked(mItem.isOdd_draw_select());
        tv_jjcProjectTwo.setTag("0_" + perfix + "_1_" + mItem.getOdd_draw() + "_平_1");

        tv_jjcProjectThree.setText(mItem.getOdd_away());
        tv_jjcProjectThree.setChecked(mItem.isOdd_away_select());
        tv_jjcProjectThree.setTag("0_" + perfix + "_2_" + mItem.getOdd_away() + "_负_0");
    }

    @OnClick(R.id.tv_jjcProjectOne)
    void one(View v) {
        tv_jjcProjectOne.setChecked(!mItem.isOdd_home_select());
        mItem.setOdd_home_select(tv_jjcProjectOne.isChecked());
        itemStateChange();
        checkData(v);
    }

    @OnClick(R.id.tv_jjcProjectTwo)
    void two(View v) {
        tv_jjcProjectTwo.setChecked(!mItem.isOdd_draw_select());
        mItem.setOdd_draw_select(tv_jjcProjectTwo.isChecked());
        itemStateChange();
        checkData(v);
    }

    @OnClick(R.id.tv_jjcProjectThree)
    void three(View v) {
        tv_jjcProjectThree.setChecked(!mItem.isOdd_away_select());
        mItem.setOdd_away_select(tv_jjcProjectThree.isChecked());
        itemStateChange();
        checkData(v);
    }

    @OnClick(R.id.betting_item_delete)
    void delete() {
        mData.remove(mItem);
        if (changed != null)
            changed.onClickSetChangedText();
    }

    /**
     * @category 判断item的添加状态
     */
    private void itemStateChange() {
        if (mItem.isOdd_home_select() || mItem.isOdd_draw_select() || mItem.isOdd_away_select()) {
            if (!mData.contains(mItem)) {
                mData.add(mItem);
            }
        } else {
            if (mData.contains(mItem)) {
                mData.remove(mItem);
            }
        }
        if (changed != null)
            changed.onClickSetChangedText();
    }

    void checkData(View v) {
        //记录点击的数据
        Map<String, Map<String, String>> cMap;
        if (mItem.getMap() == null) {
            cMap = new HashMap<>();
            mItem.setMap(cMap);
        } else {
            cMap = mItem.getMap();
        }
        String tag = v.getTag().toString();
        String key = tag.substring(0, 1);
        String value = tag;
        if (cMap.get(key) != null) {
            //有则删除 无则添加 - 等同于没选中则选中 选中了则取消
            if (cMap.get(key).get(value) != null) {
                cMap.get(key).remove(value);
            } else {
                cMap.get(key).put(value, value);
            }
        } else {
            Map<String, String> map = new HashMap<>();
            map.put(value, value);
            cMap.put(key, map);
        }
    }
}
